package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientTransferRecord;
import cn.wolfcode.crm.domain.DevelopFailedRecord;
import cn.wolfcode.crm.domain.LostClientRecord;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    //验证身份证号
    @RequestMapping("/validateIdNumber")
    @ResponseBody
    public Boolean validateIdNumber(String identitynum,Long id) {
        return clientService.validateIdNumber(identitynum,id);
    }

    @RequiresPermissions(value = {"client:saveOrUpdate", "客户新增/编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Client entity) {
        try {
            clientService.saveOrUpdate(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectAll() {
        return clientService.listAll();
    }

    //潜在客户界面
    @RequestMapping("/potentialClient")
    public String potentialClient() {
        return "client/potentialClient";
    }

    //正式客户界面
    @RequestMapping("/officialClient")
    public String officialClient() {
        return "client/officialClient";
    }

    //客户资源池界面
    @RequestMapping("/clientPool")
    public String clientPool() {
        return "client/clientPool";
    }

    @RequiresPermissions(value = {"client:query", "客户列表"}, logical = Logical.OR)
    @RequestMapping("/query")
    @ResponseBody
    public Object query(ClientQueryObject qo) {

        return clientService.query(qo);
    }

    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public Object selectByPrimaryKey(Long id) {

        return clientService.selectByPrimaryKey(id);
    }



    //移入资源池
    @RequestMapping("/moveToPool")
    @ResponseBody
    public Object moveToPool(Long id) {
        try {
            clientService.moveToPool(id);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    //尝试开发
    @RequestMapping("/exploit")
    @ResponseBody
    public Object exploit(Long id) {
        try {
            clientService.exploit(id);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    //移交客户
    @RequestMapping("/exchange")
    @ResponseBody
    public Object exchange(ClientTransferRecord record) {
        try {
            clientService.exchange(record);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    //开发失败
    @RequestMapping("/developFailed")
    @ResponseBody
    public Object developFailed(DevelopFailedRecord record) {
        try {
            clientService.developFailed(record);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    //标记客户流失
    @RequestMapping("/lost")
    @ResponseBody
    public Object lost(LostClientRecord record) {
        try {
            clientService.lost(record);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    //吸纳客户
    @RequestMapping("/absorb")
    @ResponseBody
    public Object absorb(Long id) {
        try {
            clientService.absorb(id);
        } catch (Exception e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

}
